package com.etc.springbootssm.config;

import com.etc.springbootssm.job.MyJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class QuartzConfig {


    /**
     *  返回一个JobDetail 对象交给spring容器管理
     * @return
     */
    @Bean
    public JobDetail getJobDetail(){
        //new job新的作业
        return JobBuilder.newJob(MyJob.class).withIdentity("myJob").storeDurably().build();
    }

    /**
     * CronTrigger 对象给spring来管理
     * @return
     */
    @Bean
    public CronTrigger getCronTrigger(){
        //1.获取CronScheduleBuilder对象

        //0/2 * * * * ? 每隔2秒钟执行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?").withMisfireHandlingInstructionDoNothing();

        //2.获取CronTrigger对象
        //getJobDetail  ->JobDetail
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().forJob(getJobDetail())
                .withIdentity("cronTrigger").withDescription("这是cron触发器").startNow()
                .withSchedule(cronScheduleBuilder).build();

        return cronTrigger;
    }
}
